package com.itheima.sc.impl.sms;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.sc.api.sms.SmsHomeRecommendSubjectApi;
import com.itheima.sc.mapper.sms.SmsHomeRecommendSubjectMapper;
import com.itheima.sc.pojo.sms.HomeRecommendSubject;
import com.itheima.sc.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class SmsHomeRecommendSubjectImpl implements SmsHomeRecommendSubjectApi {

    @Autowired
    private SmsHomeRecommendSubjectMapper recommendSubjectMapper;

    //添加人气推荐商品
    public void addRecommendSubjects(List<HomeRecommendSubject> recommendSubjects) {
        for (HomeRecommendSubject recommendSubject : recommendSubjects) {
            recommendSubjectMapper.insert(recommendSubject);
        }
    }

    //批量删除首页推荐
    public void deleteRecommendSubjects(List<Long> ids) {
        recommendSubjectMapper.deleteBatchIds(ids);
    }
    //分页查询首页推荐
    public PageResult selectRecommendSubjectList(Integer page, Integer pageSize, Integer recommendStatus, String SubjectName) {
        //构建分页查询条件
        Page<HomeRecommendSubject> ipage = new Page<>(page, pageSize);
        //构建条件查询
        LambdaQueryWrapper<HomeRecommendSubject> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(recommendStatus != null, HomeRecommendSubject::getRecommendStatus, recommendStatus);
        wrapper.like(StrUtil.isNotBlank(SubjectName), HomeRecommendSubject::getSubjectName, SubjectName);
        //排序
        wrapper.orderByDesc(HomeRecommendSubject::getSort);
        //查询请求
        ipage = recommendSubjectMapper.selectPage(ipage, wrapper);
        return new PageResult(page, pageSize, Convert.toInt(ipage.getTotal()),
                Convert.toInt(ipage.getTotal() / pageSize), ipage.getRecords());
    }

    //批量修改首页推荐的状态
    public void updateRecommendSubjectStatus(List<Long> ids, Integer status) {
        for (Long id : ids) {
            HomeRecommendSubject RecommendSubject = new HomeRecommendSubject();
            RecommendSubject.setId(id);
            RecommendSubject.setRecommendStatus(status);
            recommendSubjectMapper.updateById(RecommendSubject);
        }
    }

    //修改首页推荐
    public void updateRecommendSubjectSort(HomeRecommendSubject RecommendSubject) {
        recommendSubjectMapper.updateById(RecommendSubject);
    }
}
